package  
{
	import flash.text.engine.BreakOpportunity;
	import org.flixel.FlxSprite;
	import org.flixel.FlxG;
	import org.flixel.*;
	/**
	 * ...
	 * @author ...
	 */
	public class LightBall extends FlxSprite
	{
		protected var speed:int = 200;
		protected var mouseOffset:int = 15; // To account for LightBall image size (1/2 of side length)
		protected var dragAmount:int = 180;
		private var powerCount:Number = 0;
		private var loop1:FlxSound;
		
		[Embed(source = '../lib/sheet.png')] protected var BallPic:Class;
		//[Embed(source = '../lib/sound/ggj11_light_Loop1.wav')] protected var ballSound1:Class;
		
		public function LightBall(ballX:int, ballY:int) 
		{
			super(ballX, ballY, BallPic);
			//this.angularVelocity = 40;
			drag = new FlxPoint(dragAmount, dragAmount);
			loadGraphic(BallPic, true, false, 30 , 30);
			addAnimation("rotate", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19], 20);
			
			//loop1 = new FlxSound();
			//loop1.loadEmbedded(ballSound1, true);
		}
		
		override public function update():void {
			super.update();
			play("rotate");
			if (FlxG.mouse.pressed())
			{
				var toward:FlxPoint = new FlxPoint();
				toward.x = ((FlxG.mouse.x - mouseOffset) - this.x);
				toward.y = ((FlxG.mouse.y - mouseOffset) - this.y);
				
				var mag:Number = Math.sqrt(toward.x*toward.x + toward.y*toward.y);
				//trace(toward.x + "  " + toward.y);// "| origin "  + point.x + " | " + point.y );
				
				//FlxG.log(Math.abs(mag));
				
				velocity.x = (toward.x / Math.abs(mag)) * speed;
				velocity.y = (toward.y / Math.abs(mag)) * speed;
				
				
				//velocity.x = Math.min(speed * Math.exp( toward.x), 50);
				//velocity.y = Math.min(speed * Math.exp( toward.y), 50);
			}
			
			else if(FlxG.mouse.justReleased()){
				//FlxG.log(velocity.x);
				//FlxG.log(velocity.y);
				//FlxG.log(Math.abs(mag));
				
				if (Math.abs(((FlxG.mouse.x - mouseOffset) - this.x)) < 3 && Math.abs(((FlxG.mouse.x - mouseOffset) - this.x)) < 3) {
					velocity.x = 0;
					velocity.y = 0;
				}
			}
			
			//FlxG.log(powerCount);
			
			//velocity.x = 0;
			//velocity.y = 0;
			
			//if (FlxG.keys.RIGHT){
				//velocity.x = 200;
			//}
			//else if (FlxG.keys.LEFT){
				//velocity.x = -200;
			//}
			//else
				//velocity.x = 0;
			//
			//if (FlxG.keys.UP){
				//velocity.y = -200;
			//}
			//else if (FlxG.keys.DOWN) {
				//velocity.y = 200;
			//}
			//else
				//velocity.y = 0;
		}
		
		public function increasePowerCount():void
		{
			//loop1.play();
			this.powerCount = Math.min(powerCount+1,5);
		}
		
		public function decreasePowerCount():void
		{
			powerCount = 0;
		}
		
		public function enoughPower():Boolean
		{
			if (powerCount >= 5)
				return true;
			else
				return false;
			
		}
		
		public function numPower():Number
		{
			return powerCount;
		}
	}
	

}